﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Model;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Data
{
    public class orederDal
    {
        IDBHelp db = DBHelp.Create();
        Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
        StringBuilder strSql = new StringBuilder();
        #region 是否存在该记录
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int orderId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select count(1) from oreder where orderId=@orderId");
            htSyn.Add("orderId", orderId);
            return db.IsExists(strSql.ToString(), htSyn);
        }
        #endregion

        #region 获得订单列表
        /// <summary>
        /// 获得订单列表
        /// </summary>
        /// <returns></returns>
        public IList<oreder> GetModel()
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select orderId,UserID,orderName,orderDate,orderState,orderMark,isSend from oreder ");
            return getProductsBySql(strSql.ToString());
        }
        #endregion

        #region 根据订单号查询实体
        /// <summary>
        /// 根据订单号查询实体
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public oreder GetModel(int orderId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select orderId,UserID,orderName,orderDate,orderState,orderMark,isSend from oreder ");
            strSql.Append(" where orderId=@orderId ");
            htSyn.Add("@orderId", orderId);
            DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
            oreder oreder = new oreder();
            if (dt.Rows.Count>0)
            {
                oreder.orderId = Convert.ToInt32(dt.Rows[0]["orderId"]);
                oreder.UserID = Convert.ToInt32(dt.Rows[0]["UserID"]);
                oreder.orderName = dt.Rows[0]["orderName"].ToString();
                oreder.orderDate = Convert.ToDateTime(dt.Rows[0]["orderDate"]);
                oreder.orderState = Convert.ToBoolean(dt.Rows[0]["orderState"]);
                oreder.orderMark = dt.Rows[0]["orderMark"].ToString();
                oreder.isSend = Convert.ToBoolean(dt.Rows[0]["isSend"]);
            }            
            return oreder;
        }
        #endregion

        #region 修改订单信息
        /// <summary>
        /// 修改订单信息
        /// </summary>
        /// <param name="oreder"></param>
        /// <returns></returns>
        public bool Update(oreder oreder)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("update oreder set ");
            strSql.Append("UserID=@UserID,");
            strSql.Append("orderName=@orderName,");
            strSql.Append("orderDate=@orderDate,");
            strSql.Append("orderState=@orderState,");
            strSql.Append("orderMark=@orderMark,");
            strSql.Append("isSend=@isSend");
            strSql.Append(" where orderId=@orderId ");
            htSyn.Add("@orderId",oreder.orderId);
            htSyn.Add("@UserID",oreder.UserID);
            htSyn.Add("@orderName",oreder.orderName);
            htSyn.Add("@orderDate",oreder.orderDate);
            htSyn.Add("@orderState",oreder.orderState);
            htSyn.Add("@orderMark",oreder.orderMark);
            htSyn.Add("@isSend", oreder.isSend);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 删除订单根据订单号
        /// <summary>
        /// 删除订单根据订单号
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public bool Delete(int orderId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("delete from oreder where orderId=@orderId");
            htSyn.Add("orderId", orderId);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 添加订单
        /// <summary>
        /// 添加订单
        /// </summary>
        /// <param name="oreder"></param>
        /// <returns></returns>
        public bool Insert(oreder oreder)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("insert into oreder([UserID],[orderName],[orderDate],[orderState],[orderMark],[isSend]) values (@UserID,@orderName,@orderDate,@orderState,@orderMark,@isSend)");
            htSyn.Add("@orderId", oreder.orderId);
            htSyn.Add("@UserID", oreder.UserID);
            htSyn.Add("@orderName", oreder.orderName);
            htSyn.Add("@orderDate", oreder.orderDate);
            htSyn.Add("@orderState", oreder.orderState);
            htSyn.Add("@orderMark", oreder.orderMark);
            htSyn.Add("@isSend", oreder.isSend);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 根据SQL语句获取集合
        /// <summary>
        ///根据SQL语句获取集合
        /// </summary>
        public IList<oreder> getProductsBySql(string strSql)
        {
            IList<oreder> list = new List<oreder>();
            DataTable dt = db.GetDataTable(strSql);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    oreder oreder = new oreder();
                    oreder.orderId = Convert.ToInt32(dr["orderId"]);
                    oreder.UserID = Convert.ToInt32(dr["UserID"]);
                    oreder.orderName = dr["orderName"].ToString();
                    oreder.orderDate = Convert.ToDateTime(dr["orderDate"]);
                    oreder.orderState = Convert.ToBoolean(dr["orderState"]);
                    oreder.orderMark = dr["orderMark"].ToString();
                    oreder.isSend = Convert.ToBoolean(dr["isSend"]);
                    list.Add(oreder);
                }
            }
            return list;
        }
        #endregion
    }
}
